package com.hrt.web.comments.service.Impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hrt.bean.Comments;
import com.hrt.web.comments.dao.CommentsWebDao;
import com.hrt.web.comments.service.CommentsWebService;

@Transactional
@Service("commentsWebService")
public class CommentsWebServiceImpl implements CommentsWebService{
	
	@Autowired
	private CommentsWebDao commentsDao;
	
	/**
	 * 保存实体
	 * */
	@Override
	public void addComments(Comments comments) {
		this.commentsDao.saveEntity(comments);
	}

	/**
	 * 更新实体
	 * */
	@Override
	public void updateComments(Comments comments) {
		this.commentsDao.updateEntity(comments);
	}

	/**
	 * 删除实体
	 * */
	@Override
	public void delComments(Long id) {
		this.commentsDao.deleteEntity(id);
	}

	/**
	 * 按id查询实体
	 * */
	@Override
	public Comments getById(Long id) {
		
		return this.commentsDao.getById(id);
	}

	/**
	 * 查询所有
	 * */
	@Override
	public List<Comments> searchAll() {
		
		return this.commentsDao.findAll();
	}

	/**
	 * 按是否删除查询
	 */
	@Override
	public List<Comments> searchDelAll() {
		
		return this.commentsDao.findDelAll();
	}

	/**
	 * 按id列表查询实体
	 * */
	@Override
	public List<Comments> getByIds(Long[] ids) {
		
		return this.commentsDao.getByIds(ids);
	}

	/**分页*/
	@Override
	public Long totalCount() {
		
		return this.commentsDao.totalCount();
	}
	@Override
	public List<Comments> queryByPage(int pageIndex, int pageSize) {
		
		return this.commentsDao.queryByPage(pageIndex, pageSize);
	}

	/**获取数据库的当前Timestamp形式的时间*/
	@Override
	public Timestamp getDate() {
		return this.commentsDao.getDate();
	}

	/**删除选中的数据*/
	@Override
	public void deleteById(Long[] ids) throws Exception {
		List<Comments> delResult = this.commentsDao.getByIds(ids);
		for (Comments  comm: delResult) {
			comm.setIsdelete(new Integer(1));
			this.commentsDao.updateEntity(comm);
		}
	}
	
	/**父级下的分页*/
	@Override
	public List<Comments> queryById(String gdId, Integer pageIndex,
			Integer pageSize) {
		List<Map<String, Object>> conditions=new ArrayList<Map<String, Object>>();
		List<Comments> list=new ArrayList<Comments>();
		Map<String, Object> con=new HashMap<String, Object>();
		con.put("field", "id");
		con.put("value", gdId);
		
		Map<String, Object> delcon=new HashMap<String, Object>();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(con);
		list=commentsDao.queryPageByGiftId(conditions,pageIndex, pageSize);
		return list;
	}
	/**追评的分页*/
	@Override
	public List<Comments> queryAddCommById(String gdId, Integer pageIndex,
			Integer pageSize) {
		List<Map<String, Object>> conditions=new ArrayList<Map<String, Object>>();
		List<Comments> list=new ArrayList<Comments>();
		Map<String, Object> con=new HashMap<String, Object>();
		con.put("field", "id");
		con.put("value", gdId);
		
		Map<String, Object> delcon=new HashMap<String, Object>();
		delcon.put("field", "isdelete");
		delcon.put("value", 0);
		conditions.add(con);
		list=commentsDao.queryAddCommById(conditions,pageIndex, pageSize);
		return list;
	}

	/**根据礼品规格Id查找评论Id*/
	@Override
	public List<Long> findCommIdByGdeId(String gdeIds) {
		return this.commentsDao.findCommIdByGdeId(gdeIds);
	}
	
	/**根据规格id获取数量*/
	@Override
	public Long findNumByGdeId(String gdeIds){
		return commentsDao.findNumByGdeId(gdeIds);
	}
	
	/**追评   --》根据规格id获取数量*/
	@Override
	public Long findAddCommNumByGdeId(String gdeIds){
		return commentsDao.findAddCommNumByGdeId(gdeIds);
	}

}
